//
//  LuckyNumberViewController.m
//  NewProject
//
//  Created by apple on 2018/6/2.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "LuckyNumberViewController.h"

#import "LN_BirthdayView.h"

#import "LN_ZodiacView.h"
#import "LN_NumberView.h"

#import "LuckyResultView.h"

@interface LuckyNumberViewController ()
@property (weak, nonatomic) IBOutlet UIView *buttonBoxView;


@property (weak, nonatomic) IBOutlet UIView *boxView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *dateBottomCons;

@property (weak, nonatomic) IBOutlet UIButton *nextButton;


@property (weak, nonatomic) IBOutlet UIDatePicker *birthDayDatePicker;


@property(nonatomic,retain) LN_BirthdayView* birthDayView;
@property(nonatomic,retain) LN_ZodiacView* zodiacView;
@property(nonatomic,retain) LN_NumberView* lnNumberView;

@property(nonatomic,retain) LuckyResultView* resultView;

@property(nonatomic,assign)NSInteger zodiacIndex;

@end

@implementation LuckyNumberViewController{
    NSInteger lotteyType;
    NSInteger luckyType;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"幸运号码";
    lotteyType = 1;
    luckyType = 0;
    self.zodiacIndex = 0;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self initAllViews];
    });
    
}

-(void)initAllViews{
    __weak typeof(self)weakSelf = self;
    
    self.birthDayView = [[UINib nibWithNibName:@"LN_Birthday" bundle:[NSBundle mainBundle]] instantiateWithOwner:nil options:nil][0];
    self.birthDayView.frame = CGRectMake(0, 0, self.boxView.frame.size.width, self.boxView.frame.size.height);
    self.birthDayView.birthBlcok = ^{
        weakSelf.dateBottomCons.constant = 0;
        weakSelf.nextButton.hidden = YES;
    };
    
    self.zodiacView = [[UINib nibWithNibName:@"LN_Zodiac" bundle:[NSBundle mainBundle]] instantiateWithOwner:nil options:nil][0];
    self.zodiacView.frame = CGRectMake(0, 0, self.boxView.frame.size.width, self.boxView.frame.size.height);
    self.zodiacView.callbackBlock = ^(NSInteger zodiacIndex) {
        weakSelf.zodiacIndex = zodiacIndex;
    };
    
    self.lnNumberView = [[UINib nibWithNibName:@"LN_Number" bundle:[NSBundle mainBundle]] instantiateWithOwner:nil options:nil][0];
    self.lnNumberView.frame = CGRectMake(0, 0, self.boxView.frame.size.width, self.boxView.frame.size.height);
    
    
    self.resultView = [[UINib nibWithNibName:@"LuckyResult" bundle:[NSBundle mainBundle]] instantiateWithOwner:nil options:nil][0];
    self.resultView.frame = self.view.frame;
    
    [self loadTypeViewWith:0];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (IBAction)buttonAction:(UIButton *)sender {
    for (UIButton* button in self.buttonBoxView.subviews) {
        button.backgroundColor = [UIColor whiteColor];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    sender.backgroundColor = kRGBColor(255,239,228);
    [sender setTitleColor:kRGBColor(255,114,12) forState:UIControlStateNormal];
    
    
    lotteyType = sender.tag-100;
    
    
}

- (IBAction)luckyTypeAction:(UISegmentedControl *)sender {
    [self loadTypeViewWith:sender.selectedSegmentIndex];
    luckyType = sender.selectedSegmentIndex;
}

#pragma mark - 取消选择生日
- (IBAction)cancelAction:(id)sender {
    self.dateBottomCons.constant = -200;
    self.nextButton.hidden = NO;
}


#pragma mark - 选择生日
- (IBAction)doneAction:(id)sender {
    NSDate* date = [self.birthDayDatePicker date];

    NSDate* nowDate = [NSDate date];
    
    NSLog(@"compare = %ld",[nowDate compare:date]);
    
    if ([nowDate compare:date]>0) {
        NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"yyyy-MM-dd";
        NSString* dateString = [formatter stringFromDate:date];
        [self.birthDayView.birthdayButton setTitle:dateString forState:UIControlStateNormal];
        
        self.dateBottomCons.constant = -200;
        self.nextButton.hidden = NO;
    }else{
        [EasyTextView showInfoText:@"生日不要超过今天"];
        return;
    }
    

    

}

#pragma mark - 判断数字
- (BOOL) deptNumInputShouldNumber:(NSString *)str
{
    if (str.length == 0) {
        return NO;
    }
    NSString *regex = @"[0-9]*";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    if ([pred evaluateWithObject:str]) {
        return YES;
    }
    return NO;
}

#pragma mark - 获取号码
- (IBAction)getLuckyNumAction:(id)sender {
    
    switch (luckyType) {
        case 0:{
            if ([self.birthDayView.nameTextField.text length]==0) {
                [EasyTextView showErrorText:@"请输入您的姓名！"];
                return;
            }
            if ([self.birthDayView.birthdayButton.titleLabel.text isEqualToString:@"请选择您的生日"]) {
                [EasyTextView showErrorText:@"请选择您的生日！"];
                return;
            }
        }
            break;
        case 1:{
            if ([self.lnNumberView.numberTextField.text length]==0) {
                [EasyTextView showErrorText:@"请输入您的幸运数字！"];
                return;
            }
            if (![self deptNumInputShouldNumber:self.lnNumberView.numberTextField.text]) {
                [EasyTextView showErrorText:@"请输入您的幸运数字！"];
                return;
            }
        }
            break;
        case 2:{
            if (self.zodiacIndex==0) {
                [EasyTextView showErrorText:@"请选择您的幸运生肖！"];
                return;
            }
        }
            break;
        default:
            break;
    }

    NSString* lotteryID = @"";
    NSString* lotteryName = @"";
    switch (lotteyType) {
        case 1:
            lotteryID = @"4";
            lotteryName = @"超级大乐透";
            break;
        case 2:
            lotteryID = @"3";
            lotteryName = @"双色球";
            break;
        case 3:
            lotteryID = @"5";
            lotteryName = @"福彩3D";
            break;
        case 4:
            lotteryID = @"6";
            lotteryName = @"排列三";
            break;
        case 5:
            lotteryID = @"9";
            lotteryName = @"快三";
            break;
        case 6:
            lotteryID = @"7";
            lotteryName = @"11选5";
            break;
        default:
            break;
    }
    
    
    NSArray* redArray = [[SelectionNumManager shared] randomRedNumberActionWithLottoryID:lotteryID];
    NSArray* blueArray = [[SelectionNumManager shared] randomBlueNumberActionWithLottoryID:lotteryID];
    
    [self.view addSubview:self.resultView];
    self.resultView.nameLabel.text = lotteryName;
    [self.resultView loadNumberWith:redArray and:blueArray andCallBack:^(NSString *resultString) {
#pragma mark - 输出选择的号码
        if ([OpenLoginManager openLoginPageWithController:self]) {
            return;
        }
        CollectionNumberModel *model1 = [CollectionNumberModel setModelWithType:lotteryID number:resultString];
        [CollectionNumberManager collectionNumberWithArray:@[model1]];
    }];
}


-(void)loadTypeViewWith:(NSInteger)type{
    for (UIView* view in self.boxView.subviews) {
        [view removeFromSuperview];
    }
    switch (type) {
        case 0:{
            [self.boxView addSubview:self.birthDayView];
        }
            break;
        case 1:{
            [self.boxView addSubview:self.lnNumberView];
        }
            break;
        case 2:{
            [self.boxView addSubview:self.zodiacView];
        }
            break;
        default:
            break;
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
